﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;

namespace Smatime.Web.Models
{
    public class UserAccess : BaseEntity
    {
        public UserAccess()
        {
            Email = null;
            ResetCode = null;
            Password = null;
            CompanyId = null;
            RowStatus = 1;
            ResetCodeCreatedTime = null;
        }
        public string Email { get; set; }
        public string Password { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public long? CompanyId { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string ResetCode { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public DateTime? ResetCodeCreatedTime { get; set; }

        public override BaseEntity FromDataRow(DataRow row)
        {
            var listListProperties = GetType().GetProperties()
                .Where(
                    propertyInfo => (row.Table.Columns.Contains(propertyInfo.Name)) &&
                                    (row[propertyInfo.Name].HasValue()));
            foreach (var propertyInfo in listListProperties)
            {
                propertyInfo.SetValue(this, row[propertyInfo.Name]);
            }
            return base.FromDataRow(row);
        }

        public override void ToDataRow(DataRow row)
        {
            var listListProperties = GetType().GetProperties()
                .Where(propertyInfo =>
                    (propertyInfo.GetValue(this).HasValue()));
            foreach (var propertyInfo in listListProperties)
            {
                row[propertyInfo.Name] = propertyInfo.GetValue(this);
            }
            base.ToDataRow(row);
        }
    }
}